/*
entityName: my_software_blogg_metadata
rssUrl: http://tobias-ericsson-sw-development.blogspot.com/feeds/posts/default
lastModified: 

entityName: my_personal_blogg_metadata
rssUrl: http://tobias-ericsson-sw-development.blogspot.com/feeds/posts/default
lastModified:  
*/
//@Grab(group = 'org.ccil.cowan.tagsoup', module = 'tagsoup', version = '1.2')

//import groovy.xml.*


import com.google.appengine.api.datastore.Entity
import com.google.appengine.api.datastore.PreparedQuery
import com.google.appengine.api.datastore.Query
import com.google.appengine.api.urlfetch.HTTPHeader
import com.google.appengine.api.urlfetch.HTTPMethod
import com.google.appengine.api.urlfetch.HTTPRequest
import com.google.appengine.api.urlfetch.HTTPResponse
import static com.google.appengine.api.datastore.FetchOptions.Builder.withLimit

log.info "fetch RSS"
response.setContentType("text/html; charset=UTF-8")
response.setCharacterEncoding("UTF-8")


String urlString = request.getParameter("url")

def query = new Query("metadata")
query.addFilter("rssUrl", Query.FilterOperator.EQUAL, urlString)
PreparedQuery preparedQuery = datastore.prepare(query)
def metaDataEntities = preparedQuery.asList(withLimit(1))

if (metaDataEntities.size() > 0) {

    def metaDataEntity = metaDataEntities[0]

//String urlString = "http://tobias-ericsson-sw-development.blogspot.com/feeds/posts/default"
    URL url = new URL(metaDataEntity.rssUrl)
    HTTPRequest atomRequest = new HTTPRequest(url, HTTPMethod.GET)
    if (metaDataEntity.lastModified) {
        log.info "lastModified: " + metaDataEntity.lastModified
        HTTPHeader atomHeader = new HTTPHeader("If-Modified-Since", metaDataEntity.lastModified)
        atomRequest.header = atomHeader
    }
    HTTPResponse atomResponse = urlFetch.fetch(atomRequest)

    atomResponse.headers.each {
        log.info it.name + " : " + it.value
        if ("Last-Modified".equals(it.name)) {
            log.info "saving last modified: " + it.value
            metaDataEntity.lastModified = it.value
            metaDataEntity.save()
        }
    }




    if (200 == atomResponse.getResponseCode()) {

        String atomFeed = new String(atomResponse.content, "UTF-8")

        def tagsoupParser = new org.ccil.cowan.tagsoup.Parser()
//important to get rid of the html namespace
        tagsoupParser.setFeature(tagsoupParser.namespacesFeature, false)

        def slurper = new XmlSlurper(tagsoupParser)
        def feed = slurper.parseText(atomFeed)

        //datastore.withTransaction { }
        feed.entry.each { entry ->
            Entity entity = new Entity(metaDataEntity.entityName, entry.id.text().toString())
            entity.feedName = metaDataEntity.feedName
            entity.title = entry.title.text().toString()
            entity.content = entry.content.text().toString()
            entity.published = entry.published.text().toString()
            entity.updated = entry.updated.text().toString()
            if (entry.category[0]) {
                log.info("categoriesName: " + metaDataEntity.categoriesEntityName)
                Entity categoriesEntity = new Entity(metaDataEntity.categoriesEntityName, entry.category[0].@term.toString())
                categoriesEntity.name = entry.category[0].@term.toString()
                entity.category = entry.category[0].@term.toString()
                categoriesEntity.save()
            }
            /*
           String categories = "";
           entry.category.each {
               categories = categories + "," + it.@term.toString()
           }
           if (categories.size() > 0) {
               entity.categories = categories.substring(1)
           }*/

            //Key key = KeyFactory.createKey("Entry", 1)//entry.id.text().toString())

            //Entity entity = dbEntry as Entity
            //entity.key = key
            entity.save()

        }

    }
    out.println "Stored rss feed from " + urlString + " in " + metaDataEntity.entityName
}
else {
    out.println "not found in metadata, url=" + urlString
}

//request.setAttribute 'headers', mresponse.headers
//request.setAttribute 'body', mresponse.content
//forward '/WEB-INF/pages/rss.gtpl'
